/**
  *
  *  Copyright 2006 www.51Ajax.com
  *
  *  Rss Ver 1.0
  *  Author :hopesoft[琥珀]  
  *  Email  :hopesoft@msn.com
  *  Website:http://www.51ajax.com
  *  
  **/
  
	function GetRss(fid,div,num,stype,sid,webindex){
  	//stype:0不循环 1循环
		
        var url,rsslink;
	    rsslink=rssWebinfo[swebid].rsslink;	  
	    if(webindex!=undefined){rsslink=rssWebinfo[webindex].rsslink;};
		if(rsslink.toLowerCase().indexOf("http://")==-1)
		{url=rsslink.replace(/{\$rssid\$}/g,fid)+(rsslink.indexOf("?")==-1?"?":"&")+"rnd="+Math.round(Math.random()*1000,4)}
		else
		{url="/rssproxy.php?webid="+rssWebinfo[(webindex!=undefined)?webindex:swebid].webid+"&feedid="+fid+"&feed="+rsslink.replace(/{\$rssid\$}/g,fid)		
		}
		postMsg("正在读取数据，请稍等!","loading",div)	
        
        startRequest("POST",url,"", processXML);
		
		function processXML(){
		   if(typeof maxtitlelen=="undefined")maxtitlelen=36;
		   var xmldoc = http_51ajax.responseXML;		   
		   var items = xmldoc.getElementsByTagName('item');
		   var title;
		   var description;
	 	   var rss="";
		   var oBuffer = new StringBuffer();
			oBuffer.append("<div class='right'><UL>");
			var rssnum=items.length
			var totalnum=0
      		for(i=0;(totalnum<num && i<rssnum);i++){
				var descid=fid+"_"+i				
				title=items[i].getElementsByTagName("title");  //读取RSS中的title项				
				links=items[i].getElementsByTagName("link");
				totalnum+=1;	
				oBuffer.append("<li><a href='"+getXMLNode(links[0].firstChild)+"' target='_blank' title='"+getXMLNode(title[0].firstChild)+"'>"+getXMLNode(title[0].firstChild).toString().ellipse(maxtitlelen)+"</a></li>")				
			}
			oBuffer.append("</UL></div>")
		    $(div).innerHTML=oBuffer.toString();		
			if(stype=="1"){			
				if(rssWebinfo.length<=swebid){return;}
				scid+=1;
				if(rssWebinfo[swebid].channels.length>scid){GetRss(rssWebinfo[swebid].channels[scid].cid,"rss"+rssWebinfo[swebid].webid+rssWebinfo[swebid].channels[scid].cid,10,stype,sid)}
				else{					
				  if(rssWebinfo.length-1>swebid){  
					scid=0;
					swebid+=1;  				
					GetRss(rssWebinfo[swebid].channels[scid].cid,"rss"+rssWebinfo[swebid].webid+rssWebinfo[swebid].channels[scid].cid,10,stype,sid)
				  }	
				}
			}
		 }
	}

//*******************************************
//   公共函数
//*******************************************	
	function StringBuffer(){
		this._strings_= new Array;	
	}
	
	StringBuffer.prototype.append = function(str){
		this._strings_.push(str);
	}
	StringBuffer.prototype.toString = function() {
			return this._strings_.join("");
	}

	function $(obj){return document.getElementById(obj)}	
	function show(i)
		{
		if($("desc"+i).style.display=="none"){$("desc"+i).style.display="block"}
		else{$("desc"+i).style.display="none"}}

	function hideobj(id){
		if($(id)){$(id).style.display='none';}
		}

	function minmax(id){
	    var c=$("rss"+id);
    	if(c){
	        var d=c.style.display!="none";
        	var e=$("minmax_a_"+id);
	        if(e){
	            if(d){
				e.className=e.className.replace(/minbox/,"maxbox")				
				c.style.display="none";
				}
            else{
				e.className=e.className.replace(/maxbox/,"minbox")				
				c.style.display="block";
				}
    	    }

	    }
	    return false
	}

	function editobj(id,id2)	
		{	
		if($(id).style.display=="none"){$(id).style.display="block";$(id2).innerHTML="取消"}
		else{$(id).style.display="none";$(id2).innerHTML="编辑"}}

	function setlistnum(rssid,rsstype,stype,webindex){
		var listnum=$("listnum_"+rsstype+rssid).value;	
		GetRss(rssid,"rss"+rsstype+rssid,listnum,stype,0,webindex)		
		}

	function postMsg(){
		if(arguments.length==3){var mdiv=arguments[2];}else{return}
		var msg=arguments[0];
		var imgtype=arguments[1]
		var imgtype=imgtype.toLowerCase();
		var img="";		
		switch(imgtype){
			case "loading":
				img="/images/rss/loading.gif";
				break;
			case "ok":
				img="";
				break;			
			case "error":
				img="";
				break;		
			default:
				img=""
				break;	
		}
		$(mdiv).innerHTML="<div id='loading'>"+(img!=""?"<img src='"+img+"' border='0' width='20' height='20' align='absmiddle'> ":"")+msg+"</div>";	
		if(msg!="")	{
		$(mdiv).style.display=""}
		else{$(mdiv).style.display="none"}						
	}	

	function getXMLNode(obj){
			return (eval(obj)!==null)?obj.nodeValue.replace(/\</,"&lt;").replace(/\>/,"&gt;"):""
			}	
	String.prototype.ellipse = function(maxLength){
		var len=0;
		for(var i=0; i<this.length; i++){
			var c=this.charCodeAt(i);
		     //半角
    		 if(c<256||(c>=0xff61&&c<=0xff9f)){
		       len=len+1;
		     }
		     //全角
		     else{
		       len=len+2;
		     }
			 if(len>maxLength)return this.substr(0,i-3)+"...";
		   }		
    	return this;
	}
　　function chkStrLen(msg){
	    var str=msg;
		var len=0;
		for(var i=0; i<str.length; i++){
			var c=str.charCodeAt(i);
		     //半角
    		 if(c<256||(c>=0xff61&&c<=0xff9f)){
		       len=len+1;
		     }
		     //全角
		     else{
		       len=len+2;
		     }
	   }
	   return len;
	}	